//
//  HGGoodsVC.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/7/27.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGGoodsVC.h"
#import "HGGoodsCell.h"
#import "HGGoodsModel.h"
#import "HGGoodsDetailVC.h"

@interface HGGoodsVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic,strong)UITableView* tableView;


@property (nonatomic,copy)NSMutableArray<HGGoodsModel *> *goodsModels;
@end

@implementation HGGoodsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"商品列表";

    [self.tableView registerClass:[HGGoodsCell class] forCellReuseIdentifier:@"HGGoodsCell"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self initDatas];
}


- (void)initDatas {
    [[LMJRequestManager sharedManager] GET:KBaseURL(@"app/product/list") parameters:nil completion:^(LMJBaseResponse *response) {
        if (response.responseModel.status == 1) {
            _goodsModels = [HGGoodsModel mj_objectArrayWithKeyValuesArray:response.responseModel.data];
            [_tableView reloadData];
        }
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HGGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HGGoodsCell"];
    cell.goodModel = _goodsModels[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _goodsModels.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HGGoodsDetailVC *vc = [[HGGoodsDetailVC alloc] init];
    vc.goodId = _goodsModels[indexPath.row].goodsId;
    [self.navigationController pushViewController:vc animated:YES];
}


- (UITableView *)tableView {
    if (!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 150;
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview:_tableView];
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self.view);
            make.bottom.offset(-kBottomBarHeight);
        }];
    }
    return _tableView;
}

- (NSMutableArray<HGGoodsModel *> *)goodsModels {
    if (!_goodsModels) {
        _goodsModels = [@[] mutableCopy];
    }
    return _goodsModels;
}

@end
